<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------

namespace app\api\service\card;

use app\admin\model\wms\Products;
use app\admin\model\wms\Skus;
use app\api\model\card\Card;
use app\api\model\cart\Cart;
use app\core\base\BaseApiService;


/**
 *  卡牌服务层
 */
class CardService extends BaseApiService
{
     public function __construct()
     {
          parent::__construct();
          $this->model = new Card();
     }

     /**
      * 获取商品列表
      * @param array $where
      * @return array
      */
     public function getPage(array $where = [])
     {
          $field = 'id,title,sub_title,thumb,tags,rarity,money_price,0 as is_cart,ean';

          $sku_where = [
               ['status', '=', 1]
          ];

          if (!empty($where['keyword'])) {
               $sku_where[] = ['title|sub_title', 'like', '%' . $where['keyword'] . '%'];
          }


          // 参数过滤
          if (!empty($where['order']) && in_array($where['order'], ['sales', 'money_price'])) {
               $order = $where['order'] . ' ' . $where['sort'];
          } else {
               $order = 'list_weight desc,created_at desc';
          }


          $search_model = $this->model
               ->withSearch(["rarity", "goods_category", "tags"], $where)
               ->with(
                    [
                         'sku_list' => function ($query) {
                              $query->field(' id,product_id, money_price, product_spec');
                         }
                    ]

               )
               ->field($field)
               ->where($sku_where)->order($order)->append(['goods_cover_thumb_small', 'goods_cover_thumb_mid']);


          $list = $this->pageQuery($search_model);
          // 购物车列表
          if ($this->member_id > 0) {
               $cart_list = Cart::where('member_id', $this->member_id)->column('distinct(product_id) as product_id');
               foreach ($list['data'] as &$vo) {
                    // 是否添加寄售车
                    $vo['is_cart'] = in_array($vo['id'], $cart_list) ? 1 : 0;
               }
          }
          foreach ($list['data'] as &$vo) {
               $tags = explode(',', $vo['tags']);
               if (count($tags) < 2) {
                    $vo['tags'] = $tags;
               } else {
                    if ($tags['1'] == '咒术回战') {
                         $tags['1'] = 'https://kaduoduo-1325965390.cos-website.ap-guangzhou.myqcloud.com/upload/attachment/image/202406/01/1717225797fe95405876a9eae4cb9dadddb63ecd17_tencent.png';
                    } elseif ($tags['1'] == '航海王') {
                         $tags['1'] = 'https://kaduoduo-1325965390.cos-website.ap-guangzhou.myqcloud.com/upload/attachment/image/202406/01/17172258119c8d04c99fc945c50473c86abf9d45e4_tencent.png';
                    } elseif ($tags['1'] == '宝可梦') {
                         $tags['1'] = 'https://kaduoduo-1325965390.cos-website.ap-guangzhou.myqcloud.com/upload/attachment/image/202406/01/1717225805b035a7e2401eeabd9a876953dbbb1b1f_tencent.png';
                    } elseif ($tags['1'] == '七龙珠') {
                         $tags['1'] = 'https://kaduoduo-1325965390.cos-website.ap-guangzhou.myqcloud.com/upload/attachment/image/202406/01/17172258141936ff366fac47e4eaf662b4765d373a_tencent.png';
                    } elseif ($tags['1'] == '游戏王') {
                         $tags['1'] = 'https://kaduoduo-1325965390.cos-website.ap-guangzhou.myqcloud.com/upload/attachment/image/202406/01/171722581701706ca22ec37e617417dabf4b73cfa7_tencent.png';
                    }
               }
               foreach ($vo['sku_list'] as &$sku) {
                    unset($sku['product_spec']['0']);
                    $sku['product_spec'] = array_values($sku['product_spec']);
               }

               $vo['tags'] = $tags;
          }

          //dump($this->model->getLastSql());exit;

          return $list;
     }

     /**
      * 获取商品详情
      * @param array $data
      * @return array
      */
     public
     function getDetail(array $data)
     {
          //获取商品基本信息
          $info = Products::where(['id' => $data['product_id']])->find();
          $tags = explode(',', $info['tags']);
          $info['tags'] = $tags;
          $info = $info->toArray();
          if (count($tags) < 2) {
               $info['tags'] = $tags;
          } else {
               if ($tags['1'] == '咒术回战') {
                    $info['tags']['1'] = 'https://kaduoduo-1325965390.cos-website.ap-guangzhou.myqcloud.com/upload/attachment/image/202406/01/1717225797fe95405876a9eae4cb9dadddb63ecd17_tencent.png';
               } elseif ($tags['1'] == '航海王') {
                    $info['tags']['1'] = 'https://kaduoduo-1325965390.cos-website.ap-guangzhou.myqcloud.com/upload/attachment/image/202406/01/17172258119c8d04c99fc945c50473c86abf9d45e4_tencent.png';
               } elseif ($tags['1'] == '宝可梦') {
                    $info['tags']['1'] = 'https://kaduoduo-1325965390.cos-website.ap-guangzhou.myqcloud.com/upload/attachment/image/202406/01/1717225805b035a7e2401eeabd9a876953dbbb1b1f_tencent.png';
               } elseif ($tags['1'] == '七龙珠') {
                    $info['tags']['1'] = 'https://kaduoduo-1325965390.cos-website.ap-guangzhou.myqcloud.com/upload/attachment/image/202406/01/17172258141936ff366fac47e4eaf662b4765d373a_tencent.png';
               } elseif ($tags['1'] == '游戏王') {
                    $info['tags']['1'] = 'https://kaduoduo-1325965390.cos-website.ap-guangzhou.myqcloud.com/upload/attachment/image/202406/01/171722581701706ca22ec37e617417dabf4b73cfa7_tencent.png';
               }

          }
          //获取商品多个SKU的详细信息
          $skus = new Skus();
          $info['sku_list'] = $skus->where('product_id', $data['product_id'])->select()->toArray();
          foreach ($info['sku_list'] as &$sku) {
               $skus = explode(',', $sku['sku_title']);
               unset($skus['0']);
               $sku['sku_title'] = array_values($skus);
          }
          return [$info];
     }

     /**
      * 获取商品规格信息，切换规格
      * @param int $sku_id
      * @return array
      */
     public function getSku(int $sku_id)
     {

          $field = 'sku_id, sku_name, sku_image, sku_no, goods_id, sku_spec_format, price, market_price, sale_price, stock, weight, volume, sale_num, is_default';

          $goods_sku_model = new GoodsSku();

          $info = $goods_sku_model->where([['sku_id', '=', $sku_id]])
               ->field($field)
               ->with([
                    // 商品主表
                    'goods' => function ($query) {
                         $query->withField('goods_id, goods_name, goods_type, sub_title, goods_cover, unit, stock, sale_num + virtual_sale_num as sale_num, status')
                              ->append(['goods_type_name', 'goods_cover_thumb_small', 'goods_cover_thumb_mid', 'goods_cover_thumb_big']);
                    },
                    // 商品规格列表
                    'skuList' => function ($query) {
                         $query->field('sku_id, sku_name, sku_image, sku_no, goods_id, sku_spec_format, price, market_price, sale_price, stock, weight, volume, is_default');
                    },
                    // 商品规格项/规格值列表
                    'goodsSpec' => function ($query) {
                         $query->field('spec_id, goods_id, spec_name, spec_values');
                    },
               ])
               ->append(['sku_image_thumb_small', 'sku_image_thumb_mid', 'sku_image_thumb_big'])
               ->findOrEmpty()->toArray();

          return $info;
     }

     /**
      * 获取商品列表供组件调用
      * @param array $where
      * @return array
      */
     public function getGoodsComponents(array $where = [])
     {
          $field = 'goods_id,goods_name,sub_title,goods_category,goods_type,goods_cover,unit,status,sale_num + goods.virtual_sale_num as sale_num';

          $sku_where = [
               ['goodsSku.is_default', '=', 1],
               ['status', '=', 1]
          ];

          if (!empty($where['goods_ids'])) {
               $sku_where[] = ['goods.goods_id', 'in', $where['goods_ids']];
          }

          // 参数过滤
          if (!empty($where['order']) && in_array($where['order'], ['sale_num', 'price'])) {
               $order = $where['order'] . ' desc';
          } else {
               $order = 'sort desc,create_time desc';
          }

          $list = $this->model
               ->withSearch(["goods_category", "label_ids", 'service_ids'], $where)
               ->field($field)
               ->withJoin(['goodsSku'])
               ->where($sku_where)->order($order)->append(['goods_type_name', 'goods_cover_thumb_small', 'goods_cover_thumb_mid'])
               ->limit($where['num'])
               ->select()->toArray();
          return $list;
     }

}
